import db from './src/models/index.js';
import bcrypt from 'bcryptjs';

const { User } = db;

async function debugLogin() {
  try {
    // 查找用户
    const user = await User.findOne({ where: { username: 'admin' } });
    console.log('用户信息:', {
      id: user.id,
      username: user.username,
      name: user.name,
      role: user.role,
      isActive: user.isActive,
      password: user.password.substring(0, 10) + '...' // 只显示前10个字符
    });

    // 测试密码验证
    const password = 'admin123';
    const isValid = await user.validatePassword(password, user.password);
    console.log('密码验证结果:', isValid);

    // 手动验证bcrypt
    const directCompare = await bcrypt.compare(password, user.password);
    console.log('直接bcrypt比较:', directCompare);

    // 测试不同密码
    const wrongPassword = 'wrong123';
    const isWrong = await user.validatePassword(wrongPassword, user.password);
    console.log('错误密码验证:', isWrong);

  } catch (error) {
    console.error('调试失败:', error);
  }
  
  process.exit(0);
}

debugLogin(); 